<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>

	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<link rel="stylesheet" href="../layui/css/layui.css" media="all">
	<body>
		
		<blockquote class="layui-elem-quote not_border_left" style="margin-top: 10px;">
			<form class="layui-form" action="">
				<div class="layui-inline"> 
					<div class="layui-input-inline">
					    <select name="stage" id="stage" lay-filter="stage">
					    	<option value='0'>请选择阶段</option>
						</select>
					</div> 
					<div class="layui-input-inline">
					    <select name="schoolid" id="schoolid" lay-filter="schoolid">
					    	<option value='0'>请选择学校</option>
						</select>
					</div>				  
					<div class="layui-input-inline">
					    <select name="grade" id="grade" lay-filter="grade">
					    	<option value='0'>请先选择年级</option>
						</select>
					</div>
					<div class="layui-input-inline">
					    <select name="classid" id="classid" lay-filter="classid">
					    	<option value='0'>请先选择班级</option>
						</select>
					</div>
				</div>
				<button type="button" class="layui-btn" id="uploadbtn">选择多文件</button>
				<button type="button" class="layui-btn layui-btn-normal" id="testList" style="display: none;">选择多文件</button>
				<button type="button" class="layui-btn" id="testListAction">导入</button>
				<button type="button" class="layui-btn" id="savedataAction">保存</button>
			</form>
		</blockquote>

		<div class="layui-upload">
			
			<div class="layui-upload-list">
				<table class="layui-table">
					<thead>
						<tr>
							<th>文件名</th>
							<th>大小</th>
							<th>状态</th>
							<th>操作</th>
						</tr>
					</thead>
					<tbody id="demoList"></tbody>
				</table>
			</div>			
		</div>
	</body>
	<script src="../layui/layui.all.js"></script>
	<script src="../js/jquery-3.3.1.js"></script>
	<script src="../js/until/loading.js"></script>
	<script>
		layui.use('upload', function() {
			var $ = layui.jquery, upload = layui.upload, form = layui.form, layer = layui.layer, table = layui.table;

			//加载阶段下拉框
			loadstage("stage", form);
			
			//根据阶段加载学校，年级下拉框
			form.on('select(stage)', function (data) {
                //年级下拉框
                loadgrade(data.value, "grade", form);
                //学校下拉框
                loadSchool(data.value, "schoolid", form);
                
            });
            
            $.ajax({
				type: 'get',
				url: '../dataimportmanagement/deleteepqormhtdate',
				datatype: 'json',
				success: function(deldata) {},
				error: function() {
					//layer.msg("失败")
				}
			});
                   
            //根据年级和学校加载班级下拉框
            form.on('select(grade)', function (data) {
            
            	var schoolid = $("#schoolid").val();
            	if(schoolid == null || schoolid == "" || schoolid == "0"){
            		layer.alert('请先选择学校！', {icon: 2});
            		return;
            	}
            	//班级下拉框加载
            	loadClass(schoolid, data.value, "classid", form);
            
            })
            
            $("#uploadbtn").click(function(){
            	var classid = $("#classid").val();
            	if(classid == "" || classid == "0"){
            		layer.alert('请先选要导入的班级！', {icon: 2});
            		return;
            	}
            	$("#testList").click();
            })

			$("#savedataAction").click(function(){
				var index = layer.load(0, {
					shade: 0.1
				});
				var issuccess = callAJAX("get", "../dataimportmanagement/saveepqandmhtlist", {})
				if(issuccess != '' && issuccess != undefined) {
					if(issuccess.code == 10001) {
						layer.close(index);
						layer.alert(issuccess.msg, {icon: 1});
					}
					else{
						layer.close(index);
						layer.alert(issuccess.msg, {icon: 2});
					}
				}
				else{
					layer.close(index);
					layer.alert("出现重复数据或异常，数据保存失败，请检查数据后重试！", {icon: 2});
				}
			})

			$("#testList").click(function(){
				var classid = $("#classid").val();
				var schoolid = $("#schoolid").val();
				//多文件列表
				var demoListView = $('#demoList'),
				uploadListIns = upload.render({
					elem: '#testList',
					url: '../dataimportmanagement/importepqandmhtlist?classid=' + classid + '&schoolid=' + schoolid,
					accept: 'file',
					exts: 'doc|docx',
					multiple: true,				
					auto: false,
					bindAction: '#testListAction',
					choose: function(obj) {
						var files = this.files = obj.pushFile(); //将每次选择的文件追加到文件队列
						//读取本地文件
						obj.preview(function(index, file, result) {
							var tr = $(['<tr id="upload-' + index + '">', '<td>' + file.name + '</td>', '<td>' + (file.size / 1014).toFixed(1) + 'kb</td>', '<td>等待上传</td>', '<td>', '<button class="layui-btn layui-btn-xs demo-reload layui-hide">重传</button>', '<button class="layui-btn layui-btn-xs layui-btn-danger demo-delete">删除</button>', '</td>', '</tr>'].join(''));
	
							//单个重传
							tr.find('.demo-reload').on('click', function() {
								obj.upload(index, file);
							});
	
							//删除
							tr.find('.demo-delete').on('click', function() {
								delete files[index]; //删除对应的文件
								tr.remove();
								uploadListIns.config.elem.next()[0].value = ''; //清空 input file 值，以免删除后出现同名文件不可选
							});
	
							demoListView.append(tr);
						});
					},
					allDone: function(obj){ //当文件全部被提交后，才触发
					    //console.log(obj.total); //得到总文件数
					    //console.log(obj.successful); //请求成功的文件数
					    //console.log(obj.aborted); //请求失败的文件数  
					    if(obj.aborted != 0){
					    	layer.alert('一共上传' + obj.total + '个文件，成功' + obj.successful + '个文件，失败' + obj.aborted + '个文件！', {icon: 7});
					    }
					    else{
					    	layer.alert('数据全部上传成功！', {icon: 1});
					    }
					},
					done: function(res, index, upload) {
						if(res.code == 10001) { //上传成功
							var tr = demoListView.find('tr#upload-' + index),
								tds = tr.children();
							tds.eq(2).html('<span style="color: #5FB878;">上传成功</span>');
							tds.eq(3).html(''); //清空操作
							return delete this.files[index]; //删除文件队列已经上传成功的文件
						}
						this.error(index, upload);
					},
					error: function(index, upload) {
						var tr = demoListView.find('tr#upload-' + index),
							tds = tr.children();
						tds.eq(2).html('<span style="color: #FF5722;">上传失败</span>');
						tds.eq(3).find('.demo-reload').removeClass('layui-hide'); //显示重传
					}
				});
			});
			
		});
	</script>

</html>